Java Inheritance

    Java Inheritance

    Java is an object-oriented programming language thus supporting the concept of inheritance. Inheritance is a method of acquiring the behaviours and properties of its parent class. The concept behind introducing inheritance is to create new classes built on the existing classes. It allows you to reuse the methods, fields and classes. You can also modify to the existing code.

    It specifies the IS-A or parent-child relationship. It is used to achieve runtime polymorphism. It allows you to reuse the existing code to save effort and time.

    Inheritance terms

    • Class- it acts as a blueprint which creates the object. A class is referred to as the group of an object having common properties.
    • Child class- subclass or the child class will inherit the other class.
    • Parent class- it is the superclass which will get inherited by the child class.
    • Reusability- whenever you inherit the existing class it allows you to reuse the existing methods, fields, classes in the newly created class.

    Syntax-

    class Subclass-name extends Superclass-name  
    {  
       //methods and data members 
    
    }  

    In the above syntax, we use extends keyword that indicates that the superclass is being inherited by the subclass usings superclass’s functionality.

    Example-

    class Student{  
    
     float marks=40;  
    
    }  
    
    class Teacher extends Student{  
    
     int bonus=10;  
    
     public static void main(String args[]){  
    
       Student p=new Student();  
    
       System.out.println("marks"+p.marks);  
    
       System.out.println("extra marks"+p.bonus);  
    
    }  
    
    }  

    Output-

    40
    10

    Inheritance types

    The most commonly used types are- single, multilevel and hierarchical.

    Other two types- multiple and hybrid are being implemented via interfaces.

    Single inheritance

    When a class inherits another class then it is known as single inheritance. For example- crow inherits the birds class.

    Example-

    class Bird{  
      void fly(){System.out.println("flying");}  
    }  
    
    class Crow extends Bird{  
       void eat(){System.out.println("eating");}  
    }  
    
    class Demo{  
       public static void main(String args[]){  
         Crow d=new Crow();  
         d.fly();  
         d.eat();  
       }
    }  

    Output-

    flying
    
    eating

    Multilevel inheritance

    Multiple inheritances can be defined as a chain of inheritance where one class extends the other class and that class extends the other. For example- human class extended by father and then father class is extended by son class.

    Example-

    class Human{  
        void eat(){System.out.println("eating...");}  
    }  
    
    class father extends Human{  
        void scold(){System.out.println("scolding");}  
    }  
    
    class son extends father{  
        void weep(){System.out.println("weeping");}  
    }  
    
    class Demo{  
       public static void main(String args[]){  
           Son d=new son();  
           d.weep();  
           d.scold();  
           d.eat();  
       }
    }  

    Output

    weeping
    scolding
    eating

    Hierarchical inheritance

    When a single class is extended by more than one class is called hierarchical inheritance. For example- father and son both inherit the human class.

    Example-

    class Human{  
        void eat(){System.out.println("eating...");}  
    }  
    
    class father extends Human{  
        void scold(){System.out.println("scolding");}  
    }  
    
    class son extends Human{  
        void weep(){System.out.println("weeping");}  
    }  
    
    class Demo{  
        public static void main(String args[]){  
          Son d=new son();  
          d.weep();  
         //d.scold();  will not work as the son class is not inheriting the father class
         d.eat();  
       }
    }  

    Output-

    weeping
    Eating

    Multiple inheritances

    Java does not support multiple inheritances to avoid complexities. There will be a case of ambiguity when one class inherits more than one class if both classes have the same method. It means if the subclass calls the same method then which class’s method will be called.

    Example-

    class A{  
        void msg(){System.out.println("Hello");}  
    }  
    
    class B{  
        void msg(){System.out.println("Welcome");}  
    }  
    
    class C extends A,B[
        public static void main(String args[]){  
           C obj=new C();  
           obj.msg();
        }  
    }  

    Output-

    Compile-time error